package com.chuji.crmcj.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chuji.crmcj.R;
import com.chuji.crmcj.act.PotentialListAct;
import com.chuji.crmcj.act.SalesPersonActivity;
import com.chuji.crmcj.act.TenureListAct;
import com.chuji.crmcj.adapter.CustomSalesPersonAdapter;
import com.chuji.crmcj.bean.ManageIndexPeterInfo;
import com.chuji.crmcj.utils.Jump;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;
import com.chuji.crmcj.utils.VolleyUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by w.pitt on 2016/1/28.
 */
public class MagManFragment extends BaseFragment implements View.OnClickListener {
    //潜客数目
    private LinearLayout mLl_possible_client;
    private TextView mTv_possible_client_num;
    //保客数目
    private LinearLayout mLl_retain_client;
    private TextView mTv_retain_client_num;
    //更新时间
    private TextView mTv_update_time;
    //销售员列表
    private ListView mLv_salesperson;
    /*自定义适配器*/
    private CustomSalesPersonAdapter customSalesPersonAdapter;
    /*潜在客户和保有客户数目*/
    /*销售额和排名数据存储*/
    List<ManageIndexPeterInfo.ApiParamObjEntity> manageIndexPeterData;
    private ManageIndexPeterInfo manageIndexPeterInfo;
    Intent intent;
    //UserId
    String UserID;

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=View.inflate(mContext, R.layout.page_manager,null);
        //初始化数据
        mLl_possible_client = (LinearLayout)view.findViewById(R.id.ll_possible_client);
        mTv_possible_client_num = (TextView) view.findViewById(R.id.tv_possible_client_num);
        mLl_retain_client = (LinearLayout) view.findViewById(R.id.ll_retain_client);
        mTv_retain_client_num = (TextView) view.findViewById(R.id.tv_retain_client_num);
        mTv_update_time = (TextView) view.findViewById(R.id.tv_update_time);
        mLv_salesperson = (ListView) view.findViewById(R.id.lv_salesperson);
        //获取当天日期
        SimpleDateFormat formatter=new SimpleDateFormat("yyyy/MM/dd");
        Date curDate =new  Date(System.currentTimeMillis());//获取当前时间
        mTv_update_time.setText(formatter.format(curDate));
        UserID= SPUtils.getString(getContext(),"UserID","");
        //设置点击监听
        initListener();
        getManageIndexPeter();
        return view;
    }
    public void getManageIndexPeter(){
        //获得销售额和排名信息
        if (manageIndexPeterData == null) {
            manageIndexPeterData = new ArrayList<ManageIndexPeterInfo.ApiParamObjEntity>();
        }
        //初始化Volley
        String strURL = String.format(UrlUtils.GET_MAG_MANAGE+"&UserID="+"1227FC37-8E05-43ED-BE86-0CE5A401FD6F");
        StringRequest stringRequest = new StringRequest(Request.Method.GET, strURL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                manageIndexPeterInfo = JSON.parseObject(response, ManageIndexPeterInfo.class);
                manageIndexPeterData.addAll(manageIndexPeterInfo.getApiParamObj());
                if (manageIndexPeterInfo.getApiParamObj().size() != 0) {
                    //获取潜客数目
                    mTv_possible_client_num.setText(manageIndexPeterInfo.getApiParamObj().get(0).getPossibleCustomer());
                    //获取保有客户数目
                    mTv_retain_client_num.setText(manageIndexPeterInfo.getApiParamObj().get(0).getHasCustomer());
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        //初始化适配器
                        if (customSalesPersonAdapter == null) {
                            customSalesPersonAdapter = new CustomSalesPersonAdapter(getActivity(), manageIndexPeterData, mLv_salesperson);
                            mLv_salesperson.setAdapter(customSalesPersonAdapter);
                        } else {
                            customSalesPersonAdapter.notifyDataSetChanged();
                        }
                        mLv_salesperson.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                                //传递客户ID
                                intent = new Intent(UIUtils.getContext(), SalesPersonActivity.class);
                                intent.putExtra("SalesID", manageIndexPeterInfo.getApiParamObj().get(position).getSalesID());
                                intent.putExtra("avatar", manageIndexPeterInfo.getApiParamObj().get(position).getAvatar());
                                intent.putExtra("name", manageIndexPeterInfo.getApiParamObj().get(position).getRealName());
                                if(position==0){
                                    intent.putExtra("position","0");
                                }else if(position==1){
                                    intent.putExtra("position","1");
                                }else if(position==2){
                                    intent.putExtra("position","2");
                                }else{
                                    intent.putExtra("position","3");
                                }
                                if(manageIndexPeterInfo.getApiParamObj().get(position).getPerformance()!=null) {
                                    intent.putExtra("Performance", manageIndexPeterInfo.getApiParamObj().get(position).getPerformance().toString());
                                }else{
                                    intent.putExtra("Performance","0");
                                }
                                startActivity(intent);
                            }
                        });

                    }
                });
                } else {
                    UIUtils.showToastSafe("暂无数据");
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                System.out.println("失败" + error);
                UIUtils.showToastSafe("请检查网络");
            }
        });
        // Add the request to the RequestQueue.
        VolleyUtil.getRequestQueue().add(stringRequest);
    }
    @Override
    public void initData() {

    }
    @Override
    public void initListener() {
        //跳转到潜客页面
        mLl_possible_client.setOnClickListener(this);
        //跳转到保客页面
        mLl_retain_client.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_possible_client:
                Jump.toNextActivity(mContext, PotentialListAct.class);
                //设置滑动菜单不运行滑动
                break;
            case R.id.ll_retain_client:
                Jump.toNextActivity(mContext,TenureListAct.class);
                break;
        }
    }
}